package com.cy.service;

import com.cy.dao.MDao;
import com.cy.entity.Director;
import com.cy.entity.Movie;
import com.cy.entity.Star;
import com.cy.entity.Tj;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MServiceImpl implements MService{
    @Autowired
    MDao dao;

    @Override
    public PageInfo<Movie> list(Tj tj, Integer pageNum) {
        PageHelper.startPage(pageNum,3);
        List<Movie> list = dao.list(tj);
        return new PageInfo<>(list);
    }

    @Override
    public void del(Integer id) {
        dao.del(id);
        dao.delMid(id);
    }

    @Override
    public List<Director> dir() {
        return dao.dir();
    }

    @Override
    public void insert(Movie movie) {
        dao.insert(movie);
        List<Star> list = movie.getList();
        for (Star star : list) {
            String name = star.getName();
            String card = star.getCard();
            if(name!=null && card!=null && name!="" && card!=""){
                Integer i = dao.sel(star);
                if(i!=null){
                    dao.insertMid(movie.getId(),i);
                }else{
                    dao.insertStar(star);
                    dao.insertMid(movie.getId(),star.getId());
                }
            }
        }
    }

    @Override
    public Movie sel(Integer id) {
        Movie movie = dao.selMovie(id);
        return movie;
    }

    @Override
    public void update(Movie movie) {
        dao.update(movie);
        List<Star> list = movie.getList();
        dao.delMid(movie.getId());
        for (Star star : list) {
            String name = star.getName();
            String card = star.getCard();
            if(name!=null && card!=null && name!="" && card!=""){
                Integer i = dao.sel(star);
                if(i!=null){
                    dao.insertMid(movie.getId(),i);
                }else{
                    dao.insertStar(star);
                    dao.insertMid(movie.getId(),star.getId());
                }
            }
        }
    }
}
